namespace BassettData.Robots.Runner
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Runs the tasks
    /// </summary>
    public class RobotTaskRunner
    {
        /// <summary>
        /// Runs all the tasks.
        /// </summary>
        /// <param name="robotTasks">The tasks to run.</param>
        /// <returns>The description of what happened.</returns>
        public RunResult RunAllTasks(IEnumerable<IRobotTask> robotTasks)
        {
            RunResult runResult = new RunResult();

            if (robotTasks.Count() == 0)
            {
                runResult.RecordException(new InvalidOperationException("No tasks found to run!"));
            }

            // TODO implement parallel execution of the tasks.
            foreach (var robotTask in robotTasks)
            {
                try
                {
                    Console.WriteLine("Running a task.");
                    robotTask.Task();
                }
                catch (Exception ex)
                {
                    runResult.RecordException(ex);
                }
            }

            return runResult;
        }
    }
}
